package com.qidianit.mes.pro.service.impl;

import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.service.ProcessService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.common.Result;
import com.qidianit.mes.vo.ProcessPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Package Path: com.alhz.mespro.service.impl
 * Author: Alhz
 * Description: 工序
 * Date: 2025/9/17 14:05
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;


    /**
     * 分页查询工序信息
     * @param processPageVo
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result getProcessList(ProcessPageVo processPageVo,int page,int pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Process> processList = processMapper.getProcessList(processPageVo);
        PageInfo<Process> pageInfo = new PageInfo<>(processList);

        return Result.success(pageInfo);
    }

    @Override
    public Result addProcess(Process process) {
        process.setCreateTime(LocalDateTime.now());
        process.setUpdateTime(LocalDateTime.now());
        return processMapper.addProcess(process)>0?Result.success():Result.error("操作失败");
    }


    /**
     * @param code
     * @return
     */
    @Override
    public Result getProcessByCode(String code) {
        return Result.success(processMapper.getProcessByCode(code));
    }

    /**
     * 更新工序
     * @param process
     * @return
     */
    @Override
    public Result updateProcess(Process process) {
        process.setUpdateTime(LocalDateTime.now());

        System.out.println(process);
        Process process1 = processMapper.getProcessById(process.getId());

        if(process1.getProcessCode().equals(process.getProcessCode())){
            processMapper.updateProcess(process);
            return Result.success();
        }else{
            System.out.println(processMapper.getProcessByCode(process.getProcessCode()));
            if(processMapper.getProcessByCode(process.getProcessCode())!=null){
                return Result.error("操作失败，工序编号已经存在");
            }
            processMapper.updateProcess(process);
            return Result.success();

        }
    }

    @Override
    public Result deleteProcessById(Long id) {
        return processMapper.deleteProcessById(id)>0?Result.success():Result.error("操作失败");
    }

    @Override
    public Result batchDeleteProcess(Long[] ids) {
        return processMapper.batchDeleteProcess(ids)>0?Result.success():Result.error("操作失败");
    }
}
